﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Catering_DBOpertion;
using System.Collections.Generic;//Please add references
namespace Catering_Operation
{
    /// <summary>
    /// 数据访问类:FromRecords
    /// </summary>
    public partial class FromRecords
    {
        public FromRecords()
        { }
        public List<string> GetYears()
        {
            List<string> listDTime = new List<string>();
            string sql = "select year(OutDate) as years from FromRecords GROUP BY (year(OutDate)) having count(OutDate) >=1";
            DataTable dt = DbHelperSQL.Query(sql).Tables[0];
            if (dt != null && dt.Rows.Count >= 1)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int temp = 0;
                    Int32.TryParse(dt.Rows[i]["years"].ToString(), out temp);
                    if (temp > 0)
                    {
                        listDTime.Add(temp.ToString());
                    }
                }
            }
            return listDTime;
        }

        /// <summary>
        /// 获取消耗口径
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public decimal GetAmountMoney(string where)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(" select sum(Amountmoney) as Amountmoney from FromRecords where 1=1");
            sql.Append(where);
            DataTable dt = DbHelperSQL.Query(sql.ToString()).Tables[0];
            decimal AmountMoney = 0;
            if (dt != null && dt.Rows.Count > 0)
            {
                if (!(dt.Rows[0]["AmountMoney"] is DBNull) && !string.IsNullOrEmpty(dt.Rows[0]["AmountMoney"].ToString())) AmountMoney = Convert.ToDecimal(dt.Rows[0]["AmountMoney"].ToString());
            }
            return AmountMoney;
        }

        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("id", "FromRecords");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from FromRecords");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Catering_Model.FromRecords model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into FromRecords(");
            strSql.Append("id,GroupID,ClassID,DBID,RAWTID,RAWIDs,RAWID,RAWName,Price,Numbers,AmountMoney,CMP,OutDate,Back,temp1,temp2,Specifications,InDBid,SumNumber)");
            strSql.Append(" values (");
            strSql.Append("@id,@GroupID,@ClassID,@DBID,@RAWTID,@RAWIDs,@RAWID,@RAWName,@Price,@Numbers,@AmountMoney,@CMP,@OutDate,@Back,@temp1,@temp2,@Specifications,@InDBid,@SumNumber)");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@GroupID", SqlDbType.Int,4),
					new SqlParameter("@ClassID", SqlDbType.Int,4),
					new SqlParameter("@DBID", SqlDbType.Int,4),
					new SqlParameter("@RAWTID", SqlDbType.Int,4),
					new SqlParameter("@RAWIDs", SqlDbType.Int,4),
					new SqlParameter("@RAWID", SqlDbType.Int,4),
					new SqlParameter("@RAWName", SqlDbType.VarChar,50),
					new SqlParameter("@Price", SqlDbType.Decimal,9),
					new SqlParameter("@Numbers", SqlDbType.VarChar,100),
					new SqlParameter("@AmountMoney", SqlDbType.Decimal,9),
					new SqlParameter("@CMP", SqlDbType.VarChar,100),
					new SqlParameter("@OutDate", SqlDbType.DateTime),
					new SqlParameter("@Back", SqlDbType.VarChar,200),
					new SqlParameter("@temp1", SqlDbType.VarChar,200),
					new SqlParameter("@temp2", SqlDbType.VarChar,200),
					new SqlParameter("@Specifications", SqlDbType.VarChar,100),
					new SqlParameter("@InDBid", SqlDbType.VarChar,50),
                    new SqlParameter("@SumNumber", SqlDbType.Decimal,20)};
            parameters[0].Value = model.id;
            parameters[1].Value = model.GroupID;
            parameters[2].Value = model.ClassID;
            parameters[3].Value = model.DBID;
            parameters[4].Value = model.RAWTID;
            parameters[5].Value = model.RAWIDs;
            parameters[6].Value = model.RAWID;
            parameters[7].Value = model.RAWName;
            parameters[8].Value = model.Price;
            parameters[9].Value = model.Numbers;
            parameters[10].Value = model.AmountMoney;
            parameters[11].Value = model.CMP;
            parameters[12].Value = model.OutDate;
            parameters[13].Value = model.Back;
            parameters[14].Value = model.temp1;
            parameters[15].Value = model.temp2;
            parameters[16].Value = model.Specifications;
            parameters[17].Value = model.InDBid;
            parameters[18].Value = model.SumNumber;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Catering_Model.FromRecords model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update FromRecords set ");
            strSql.Append("GroupID=@GroupID,");
            strSql.Append("ClassID=@ClassID,");
            strSql.Append("DBID=@DBID,");
            strSql.Append("RAWTID=@RAWTID,");
            strSql.Append("RAWIDs=@RAWIDs,");
            strSql.Append("RAWID=@RAWID,");
            strSql.Append("RAWName=@RAWName,");
            strSql.Append("Price=@Price,");
            strSql.Append("Numbers=@Numbers,");
            strSql.Append("AmountMoney=@AmountMoney,");
            strSql.Append("CMP=@CMP,");
            strSql.Append("OutDate=@OutDate,");
            strSql.Append("Back=@Back,");
            strSql.Append("temp1=@temp1,");
            strSql.Append("temp2=@temp2,");
            strSql.Append("Specifications=@Specifications,");
            strSql.Append("InDBid=@InDBid");
            strSql.Append("SumNumber=@SumNumber");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@GroupID", SqlDbType.Int,4),
					new SqlParameter("@ClassID", SqlDbType.Int,4),
					new SqlParameter("@DBID", SqlDbType.Int,4),
					new SqlParameter("@RAWTID", SqlDbType.Int,4),
					new SqlParameter("@RAWIDs", SqlDbType.Int,4),
					new SqlParameter("@RAWID", SqlDbType.Int,4),
					new SqlParameter("@RAWName", SqlDbType.VarChar,50),
					new SqlParameter("@Price", SqlDbType.Decimal,9),
					new SqlParameter("@Numbers", SqlDbType.VarChar,100),
					new SqlParameter("@AmountMoney", SqlDbType.Decimal,9),
					new SqlParameter("@CMP", SqlDbType.VarChar,100),
					new SqlParameter("@OutDate", SqlDbType.DateTime),
					new SqlParameter("@Back", SqlDbType.VarChar,200),
					new SqlParameter("@temp1", SqlDbType.VarChar,200),
					new SqlParameter("@temp2", SqlDbType.VarChar,200),
					new SqlParameter("@Specifications", SqlDbType.VarChar,100),
					new SqlParameter("@InDBid", SqlDbType.VarChar,50),
                    new SqlParameter("@SumNumber", SqlDbType.Decimal,20),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.GroupID;
            parameters[1].Value = model.ClassID;
            parameters[2].Value = model.DBID;
            parameters[3].Value = model.RAWTID;
            parameters[4].Value = model.RAWIDs;
            parameters[5].Value = model.RAWID;
            parameters[6].Value = model.RAWName;
            parameters[7].Value = model.Price;
            parameters[8].Value = model.Numbers;
            parameters[9].Value = model.AmountMoney;
            parameters[10].Value = model.CMP;
            parameters[11].Value = model.OutDate;
            parameters[12].Value = model.Back;
            parameters[13].Value = model.temp1;
            parameters[14].Value = model.temp2;
            parameters[15].Value = model.Specifications;
            parameters[16].Value = model.InDBid;
            parameters[17].Value = model.SumNumber;
            parameters[18].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from FromRecords ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from FromRecords ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Catering_Model.FromRecords GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,GroupID,ClassID,DBID,RAWTID,RAWIDs,RAWID,RAWName,Price,Numbers,AmountMoney,CMP,OutDate,Back,temp1,temp2,Specifications,InDBid,SumNumber from FromRecords ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            Catering_Model.FromRecords model = new Catering_Model.FromRecords();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["GroupID"].ToString() != "")
                {
                    model.GroupID = int.Parse(ds.Tables[0].Rows[0]["GroupID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ClassID"].ToString() != "")
                {
                    model.ClassID = int.Parse(ds.Tables[0].Rows[0]["ClassID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DBID"].ToString() != "")
                {
                    model.DBID = int.Parse(ds.Tables[0].Rows[0]["DBID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RAWTID"].ToString() != "")
                {
                    model.RAWTID = int.Parse(ds.Tables[0].Rows[0]["RAWTID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RAWIDs"].ToString() != "")
                {
                    model.RAWIDs = int.Parse(ds.Tables[0].Rows[0]["RAWIDs"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RAWID"].ToString() != "")
                {
                    model.RAWID = int.Parse(ds.Tables[0].Rows[0]["RAWID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RAWName"] != null)
                {
                    model.RAWName = ds.Tables[0].Rows[0]["RAWName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Price"].ToString() != "")
                {
                    model.Price = decimal.Parse(ds.Tables[0].Rows[0]["Price"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Numbers"] != null)
                {
                    model.Numbers = ds.Tables[0].Rows[0]["Numbers"].ToString();
                }
                if (ds.Tables[0].Rows[0]["AmountMoney"].ToString() != "")
                {
                    model.AmountMoney = decimal.Parse(ds.Tables[0].Rows[0]["AmountMoney"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CMP"] != null)
                {
                    model.CMP = ds.Tables[0].Rows[0]["CMP"].ToString();
                }
                if (ds.Tables[0].Rows[0]["OutDate"].ToString() != "")
                {
                    model.OutDate = DateTime.Parse(ds.Tables[0].Rows[0]["OutDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Back"] != null)
                {
                    model.Back = ds.Tables[0].Rows[0]["Back"].ToString();
                }
                if (ds.Tables[0].Rows[0]["temp1"] != null)
                {
                    model.temp1 = ds.Tables[0].Rows[0]["temp1"].ToString();
                }
                if (ds.Tables[0].Rows[0]["temp2"] != null)
                {
                    model.temp2 = ds.Tables[0].Rows[0]["temp2"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Specifications"] != null)
                {
                    model.Specifications = ds.Tables[0].Rows[0]["Specifications"].ToString();
                }
                if (ds.Tables[0].Rows[0]["InDBid"] != null)
                {
                    model.InDBid = ds.Tables[0].Rows[0]["InDBid"].ToString();
                }

                if (ds.Tables[0].Rows[0]["SumNumber"].ToString() != "")
                {
                    model.SumNumber = decimal.Parse(ds.Tables[0].Rows[0]["SumNumber"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,GroupID,ClassID,DBID,RAWTID,RAWIDs,RAWID,RAWName,Price,Numbers,AmountMoney,CMP,OutDate,Back,temp1,temp2,Specifications,InDBid,SumNumber ");
            strSql.Append(" FROM FromRecords ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 根据条件获取最新数据的时间
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public string GetLastDate(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT TOP 1 OutDate FROM FromRecords WHERE 1=1 ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" " + strWhere + " ");
            }
            strSql.Append(" ORDER BY OutDate DESC ");
            Object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj != null) return obj.ToString(); else return "";
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,GroupID,ClassID,DBID,RAWTID,RAWIDs,RAWID,RAWName,Price,Numbers,AmountMoney,CMP,OutDate,Back,temp1,temp2,Specifications,InDBid,SumNumber ");
            strSql.Append(" FROM FromRecords ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "FromRecords";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/
        public DataSet FromRecordsListGroupByMonth(string whereclauese)
        {
            string sql = "select groupid,classid,year(outdate) as fyear,month(outdate) as fmonth,sum(amountmoney) as amountmoney" +
                " from FromRecords where 1=1 " + whereclauese + " group by groupid,classid,year(outdate),month(outdate)";
            return DbHelperSQL.Query(sql);
        }
        public DataSet FromRecordsMoneyCount(string whereclauese)
        {
            string sql = "select sum(amountmoney) as amountmoney" +
                " from FromRecords where 1=1 " + whereclauese;
            return DbHelperSQL.Query(sql);
        }


        public DataTable GetYuanLiaoChengBenGroupByMonthAndRAWTID(string whereClause)
        {
            string sql = "SELECT RAWTID,SUM(AmountMoney) AS AmountMoney,year(OutDate) AS YearDay,Month(OutDate) AS MonthDay " +
                " FROM View_YuanLiaoChengBen WHERE 1=1 " + whereClause + " GROUP BY RAWTID,year(OutDate),Month(OutDate)";
            return DbHelperSQL.Query(sql).Tables[0];
        }

        public DataTable GetChengBenGroupByMonth(string whereClause)
        {
            string sql = "SELECT SUM(AmountMoney) AS AmountMoney,year(OutDate) AS YearDay,Month(OutDate) AS MonthDay " +
                    " FROM View_YuanLiaoChengBen WHERE 1=1 " + whereClause + " GROUP BY year(OutDate),Month(OutDate)";
            return DbHelperSQL.Query(sql).Tables[0];
        }


        public int FromRecordsMoney(string whereclause)
        {
            DataTable dt = new DataTable();
            string sql = "SELECT SUM(CONVERT(decimal, Numbers) * CONVERT(decimal, Price)) AS Numbers FROM FromRecords WHERE 1=1 " + whereclause;
            object obj = DbHelperSQL.GetSingle(sql);
            return obj == null ? 0 : Convert.ToInt32(obj);
        }

        #endregion  Method

        public DataSet GetQuickFromRecordsData(string StrWhere, string NotIn, string OrderBy)
        {
            string sql = "SELECT *,ROW_NUMBER() OVER( ORDER BY ID) AS RowNo FROM (SELECT F.*,ROW_NUMBER() OVER(PARTITION BY RAWID ORDER BY ID) AS rownum ";
            sql += " FROM VIEW_QuickFromRecords AS F WHERE 1=1 ";
            if (!string.IsNullOrEmpty(StrWhere))
            {
                sql += StrWhere;
            }
            sql += " ) AS Tab WHERE rownum=1 ";
            if (!string.IsNullOrEmpty(NotIn))
            {
                sql += " " + NotIn + " ";
            }
            if (!string.IsNullOrEmpty(OrderBy))
            {
                sql += " ORDER BY " + OrderBy;
            }
            return DbHelperSQL.Query(sql);
        }

        public DataSet GetQuickFromRecordsData(string StrWhere, string NotIn, string OrderBy, int Size, int CurrentIndex, out int SumCount)
        {
            SumCount = 0;
            CurrentIndex = CurrentIndex < 1 ? 1 : CurrentIndex;
            string sql1 = "SELECT Count(*) FROM (SELECT F.*,ROW_NUMBER() OVER(PARTITION BY RAWID ORDER BY ID) AS rownum ";
            sql1 += " FROM VIEW_QuickFromRecords AS F WHERE 1=1 ";
            if (!string.IsNullOrEmpty(StrWhere))
            {
                sql1 += StrWhere;
            }
            sql1 += " ) AS Tab WHERE rownum=1 ";
            if (!string.IsNullOrEmpty(NotIn))
            {
                sql1 += " " + NotIn + " ";
            }
            object obj = DbHelperSQL.GetSingle(sql1);
            if (obj != null) Int32.TryParse(obj.ToString(), out SumCount);

            int MaxPageCount = SumCount / Size;
            MaxPageCount = SumCount % Size > 0 ? (MaxPageCount + 1) : MaxPageCount;
            CurrentIndex = CurrentIndex > MaxPageCount ? MaxPageCount : CurrentIndex;

            string sql = "SELECT Top " + Size + " * FROM (SELECT *,ROW_NUMBER() OVER( ORDER BY ID) AS RowNo FROM (SELECT F.*,ROW_NUMBER() OVER(PARTITION BY RAWID ORDER BY ID) AS rownum ";
            sql += " FROM VIEW_QuickFromRecords AS F WHERE 1=1 ";
            if (!string.IsNullOrEmpty(StrWhere))
            {
                sql += StrWhere;
            }
            sql += " ) AS Tab WHERE rownum=1 ";
            if (!string.IsNullOrEmpty(NotIn))
            {
                sql += " " + NotIn + " ";
            }
            //if (!string.IsNullOrEmpty(OrderBy))
            //{
            //    sql += " ORDER BY " + OrderBy;
            //}
            sql += ") Tab1 WHERE RowNo BETWEEN " + (Size * (CurrentIndex - 1) + 1) + " AND " + (Size * CurrentIndex) + " ORDER BY " + OrderBy;
            return DbHelperSQL.Query(sql);
        }

        #region 出库状态

        public DataSet GetDataStateByFromRecordsNew(string where)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("SELECT * FROM View_DataStates01 ");
            if (!string.IsNullOrEmpty(where)) sql.Append("WHERE " + where);
            return DbHelperSQL.Query(sql.ToString());
        }

        #endregion
    }
}

